home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / comm / tlx_sq15.zip / GLOBAL.SLT < prev    next >
Text File  |  1990-10-17  |  8KB  |  233 lines

  1. //-----------------------------------------------------------
  2. // GLOBAL.SL? Save global variables in Telix.
  3. //
  4. // Calling sequence:
  5.  
  6. // Read the variable "NAME", result in <variable string>:
  7.  
  8. //    call ("GLOBAL","R","NAME",<variable string>);
  9. //    If "NAME" is not found, an empty string is returned.
  10.  
  11. // Write the string <variable string> into "NAME":
  12.  
  13. //    call ("GLOBAL","W","NAME",<variable string>);
  14. //    If "NAME" can not be stored (not enough room) the variable
  15. //    will not be stored, and the script will return the code -1.
  16.  
  17. // Delete a variable:
  18.  
  19. //    call ("GLOBAL","W","NAME",0);      // (the "0" must be included)
  20. // or call ("GLOBAL","D","NAME",0);      // (the "0" may be omitted)
  21.  
  22. // In variable names, case is insignificant. The variables may be
  23. // *any* string, except the length is limited to 80 characters 
  24. // The script can store up to 40 variables of length <=20 characters,
  25. // and 10 variables of 20 < length <=80 characters,
  26. // Make sure that the longest length you'll ever use is specified
  27. // the first time you define a variable. Otherwise, if you want to
  28. // modify a <20 char. variable to >20 characters, you must delete
  29. // the original first.
  30.  
  31. // Author: Inge Vabekk
  32. //         Hamangskogen 108
  33. //         N-1300 SANDVIKA
  34. //         NORWAY
  35. //-----------------------------------------------------------
  36.  
  37. str shortnames[240]     // Table of names for 40 short strings
  38.    ,longnames [60]      // Table of names for 10 long strings
  39.    ,shortstrings [840]  // 40 strings of length 21
  40.    ,longstrings [810]   // 10 strings of length 81
  41.    ,helpname [6]        // Temporary for name
  42.    ,spaces   [6]        // Store spaces.
  43.    ,helpvar [81]        // Temporary for string 
  44.    ;                    // with (room for terminating zero)
  45.  
  46. int next_short_entry=0
  47.    ,next_long_entry=0
  48.    ,max_short_entry=819 // Max. subscript in entry table
  49.    ,max_long_entry=72   // Max. subscript in entry table
  50.    ,namelen             // Length of names
  51.    ,longstringlen=81    // Length of strings.
  52.    ,shortstringlen=21
  53.    ,varlen              // Length of current string.
  54.    ,long                // Long/short indocator    
  55.    ,get = 0             // Code to GET a variable.
  56.    ,put = 1             // Code to PUT a variable.
  57.    ;
  58.  
  59. //-----------------------------------------------------------
  60. // Script starts here.
  61. //-----------------------------------------------------------
  62.  
  63. main (str rwrite, str name, str varname)
  64. {
  65. int i, j;
  66.  
  67.   namelen = strmaxlen(helpname);
  68.   varlen = strlen(varname);
  69.   for(i=0; i<namelen; ++i)
  70.     setchr(spaces,i,' ');
  71.   setchr(spaces,namelen,0);
  72.  
  73.   if (rwrite=="R")
  74.     i = read (name,varname);             // Read the value.
  75.    
  76.   else if (rwrite=="W")
  77.   { if (strlen(varname) < 1)             // No name: Delete it.
  78.       i = write (name,"");
  79.     else
  80.     { i = write (name,"");               // Delete first, then 
  81.       i = write (name,varname);          // write this name.       
  82.     }
  83.   }
  84.   else if (rwrite=="D")
  85.     i = write (name,"");
  86.    
  87.   else if (rwrite=="L")                  // List all names.
  88.   { prints ("ALL SHORT GLOBAL VARIABLES:");
  89.     for (i=j=0; i<next_short_entry; i=i+namelen) 
  90.     { substr (shortstrings,j,shortstringlen,helpvar);
  91.       substr (shortnames,i,namelen,helpname);
  92.       printsc(helpname);
  93.       printsc (":");
  94.       prints (helpvar);
  95.       j = j+shortstringlen;
  96.     }
  97.     prints ("ALL LONG GLOBAL VARIABLES:");
  98.     for (i=j=0; i<next_long_entry; i=i+namelen) 
  99.     { substr (longstrings,j,longstringlen,helpvar);
  100.       substr (longnames,i,namelen,helpname);
  101.       printsc(helpname);
  102.       printsc (":");
  103.       prints (helpvar);
  104.       j = j+longstringlen;
  105.     }
  106.   }
  107.   return (i);                            // Return with code.
  108. }
  109.  
  110. //-----------------------------------------------------------
  111. // Read a variable.
  112. //-----------------------------------------------------------
  113.  
  114. read (str name, str varname)
  115. {
  116.   move (name);                           // Move name to global
  117.   return (getput (0,varname));           // and read.
  118. }
  119.  
  120. //-----------------------------------------------------------
  121. // Write a variable.
  122. //-----------------------------------------------------------
  123.  
  124. write(str name, str varname)
  125. {
  126.   move (name);                           // Move name to global
  127.   return (getput (1,varname));           // and write.
  128. }
  129.  
  130. //-----------------------------------------------------------
  131. // Move name to global area.
  132. //-----------------------------------------------------------
  133.  
  134. move(str name)
  135. {
  136.   helpname = name;                       // Move name to global.
  137.   strupper(helpname);                    // Make upper characters
  138.   strcat (helpname,spaces);              // fill with spaces
  139. }
  140.  
  141. //-----------------------------------------------------------
  142. // Read or write a name to or from the storage area.
  143. //-----------------------------------------------------------
  144.  
  145. getput (int write, str varname)
  146. {
  147. int c, i, j, exist;
  148.  
  149.   exist = strpos (longnames,helpname,0); // Check if name is in table.
  150.   if (exist >= 0)
  151.     long = 1;
  152.   else
  153.   { long = 0;
  154.     exist = strpos (shortnames,helpname,0); // Check if name is in table.
  155.   }
  156.  
  157.   if (exist < 0)                         // Non-existent?
  158.   { if (!write)                          // Read?
  159.     { varname = "";                      // Yes. Nothing there.
  160.       return(-1);
  161.     }
  162.     long = varlen > 20;
  163.     if (long)                            // Add long variable
  164.     { exist = strpos (longnames,spaces,0);// Deleted variable found?
  165.       if (exist >= 0)                    // Must be at correct position!
  166.         exist = ((exist+namelen-1)/namelen)*namelen;
  167.       else                               // No, expand table.
  168.       { exist = next_long_entry;
  169.         next_long_entry = next_long_entry+namelen;
  170.         if (next_long_entry > max_long_entry) return (-1);
  171.       }
  172.                                          // Move all characters.  
  173.       copychrs (helpname,longnames,exist,namelen);
  174.     }
  175.     else                                 // Add short variable
  176.     { exist = strpos (shortnames,spaces,0);// Deleted variable found?
  177.       if (exist >= 0)                    // Must be at correct position!
  178.         exist = ((exist+namelen-1)/namelen)*namelen;
  179.       else                               // No, expand table.
  180.       { exist = next_short_entry;
  181.         next_short_entry = next_short_entry+namelen;
  182.         if (next_short_entry > max_short_entry) return (-1);
  183.       }
  184.                                          // Move all characters.  
  185.       copychrs (helpname,shortnames,exist,namelen);
  186.     }
  187.   }
  188.  
  189. // "exist"   contains start subscript for the variable name.
  190. // "j"       contains start subscript for its contents.
  191. // "long"=1; Variable is in "longstrings
  192. // "long"=0; Variable is in "shortstrings
  193.  
  194.   if (long)
  195.   { j = (exist/namelen)*longstringlen;   // Find start position.
  196.  
  197.     if (write)                           // Write?
  198.     { if (strlen(varname)==0)            // Yes. Delete?
  199.         copychrs (spaces                 // Write over with spaces.
  200.           ,longnames,exist,namelen);
  201.       else
  202.         copystr (varname                 // Copy name.
  203.           ,longstrings,j,longstringlen-1);
  204.     }
  205.     else                                 // Read:
  206.       substr (longstrings                // Copy variable.
  207.         ,j,longstringlen,varname);
  208.   }
  209.   else
  210.   { j = (exist/namelen)*shortstringlen;  // Find start position.
  211.  
  212.     if (write)                           // Write?
  213.     { if (strlen(varname)==0)            // Yes. Delete?
  214.         copychrs (spaces                 // Write over with spaces.
  215.           ,shortnames,exist,namelen);
  216.       else
  217.         copystr (varname                 // Copy name.
  218.           ,shortstrings,j,shortstringlen-1);
  219.     }
  220.     else                                 // Read:
  221.       substr (shortstrings               // Copy variable.
  222.         ,j,shortstringlen,varname);
  223.   }
  224.   if (write) return (0);
  225.  
  226.   for (i=strlen(varname)-1; i>0; --i)
  227.   { if (subchr (varname,i) > ' ') break;
  228.     setchr (varname,i,